package com.bigdata.topN

import java.sql.{Connection, PreparedStatement}

import scala.collection.mutable.ListBuffer

object ArticleDAO {
  def insertResult(list: ListBuffer[Article]): Unit = {
    var connection: Connection = null
    var pstmt: PreparedStatement = null

    try {
      connection = MySQLUtils.getConnection()

      connection.setAutoCommit(false) //设置手动提交

      val sql = "insert into bigdata_article_topn(articleId,weight,time) values (?,?,?) "
      pstmt = connection.prepareStatement(sql)

      for (ele <- list) {
        pstmt.setString(1, ele.articleId)
        pstmt.setString(2, ele.weight.toString)
        pstmt.setString(3,ele.time)

        pstmt.addBatch()
      }

      pstmt.executeBatch() // 执行批量处理
      connection.commit() //手工提交
    } catch {
      case e: Exception => e.printStackTrace()
    } finally {
      MySQLUtils.release(connection, pstmt)
    }

}


}
